package com.project.restaurant.mapper;

import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.project.restaurant.pojo.Order;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.*;

/**
 * <p>
 *  Mapper 接口
 * </p>
 *
 * @author 作者
 * @since 2022-10-11
 */
@Mapper
public interface OrderMapper extends BaseMapper<Order> {

    /**
     * 插入一条订单数据，返回订单id
     * before: 指定sql语句是插入前执行还是插入后执行,如果是自动增长id，则为true，如果为false，那么是自定义id和自动生成id，才能返回插入id
     * keyProperty:是当返回的id，赋值给order的哪个属性
     * @param order 订单数据
     * @return 返回订单id
     */
    @Insert("insert into `order`(order_id,user_id,seat_id,transaction_id,total_price,order_demand,order_state,order_purchase_time) " +
            "VALUES(#{et.orderId},${et.userId},${et.seatId},#{et.transactionId},${et.totalPrice},#{et.orderDemand},${et.orderState},#{et.orderPurchaseTime})")
    @SelectKey(statement = "SELECT UUID();", keyProperty = "et.orderId", before = true, resultType = String.class)
    long insertOne(@Param(Constants.ENTITY) Order order);


    @Update("update `order` set order_state = ${orderState} where order_id = #{orderId}")
    int updateOrderState(@Param("orderId") String orderId,@Param("orderState") int orderState);

    @Update("<script>" +
            "update `order` set " +
            "<if test='et.transactionId != null'>" +
                "transaction_id = #{et.transactionId}," +
            "</if>" +
            "<if test='et.orderState != null'>" +
                "order_state = #{et.orderState}," +
            "</if>" +
            "order_id = order_id " +
            "where order_id = #{et.orderId}" +
            "</script>")
    int updateNotNull(@Param(Constants.ENTITY)Order order);

    @Update("update `order` set transaction_id = #{transactionId} where order_id = #{orderId}")
    int updateTransactionId(@Param("orderId") String orderId,@Param("transactionId") String transactionId);

}
